/*保持登录状态并且更新信息 */


/* 判断是否两周内保持登录状态 */
if (!$.cookie("qqt-access-token") && $.cookie("qqt-stayLogin") == "true") {
    var query = {
        tenantId: "000000",
        grant_type: "refresh_token",
        refresh_token: $.cookie("qqt-refresh-token")
    }
    utils.ajaxPost("/blade-front-auth/oauth/token?" + $.param(query), {}, function(res) {
        console.log(res)
        if (res.access_token) {
            utils.setLoginCookie("login-info", res, '')

            getCompanyList()
        }

    }, function(res) {})
}

function changeCompany(key) {

    utils.ajaxPost("/scrm/homepage/last-login-record", {
        memberId: companyList[key].memberId
    }, function(res) {
        $('#company_name').text(companyList[key].companyName.length > 8 ? companyList[key].companyName.substr(0, 8) + '...' : companyList[key].companyName).addClass('change_company')
        $(".header_company_list").hide()
            // var login_info = JSON.parse($.cookie("qqt-login-info"))
            // login_info.member_id = companyList[key].memberId;
            // // utils.setLoginCookie("company-info", companyList[key])
            // utils.setLoginCookie("login-info", login_info, $.cookie("qqt-stayLogin") == "true")
        utils.ajaxGet("/member/member-base-info?" + $.param({
                memberId: companyList[key].memberId
            }), '', function(resp) {
                console.log(resp.data)
                    //$.cookie("member-base-info",JSON.stringify(res.data), { expires: 1, path: '/' })
                utils.setLoginCookie("member-info", resp.data)
                window.location.reload()
                    //   jumpPage(isJump)
            }, function() {

            })
            //  window.layer && layer.closeAll('loading');

    })

}

function getCompanyList() {
    utils.ajaxGet("/scrm/homepage/login-enterprise", {}, function(res) {
            var companyEle = $(".header_company_list").on('click', 'li', function() {
                $(this).addClass('active').siblings().removeClass('active')
                window.layer && layer.load();
                changeCompany($(this).attr('data-key'))

            }).on('click', function(event) {
                var e = arguments.callee.caller.arguments[0] || event; //若省略此句，下面的e改为event，IE运行可以，但是其他浏览器就不兼容

                if (e && e.stopPropagation) {
                    // this code is for Mozilla and Opera
                    e.stopPropagation();

                } else if (window.event) {
                    // this code is for IE
                    window.event.cancelBubble = true;

                }
            })
            if (res.data.length === 0) {
                $(".yidenglu_li").show().children("a:eq(0)").text(JSON.parse($.cookie("qqt-login-info")).phone);
            } else {
                companyList = res.data;
                $.each(res.data, function(k, v) {
                    var ele = $('<li data-key="' + k + '">' + v.companyName + '</li>');

                    if (v.lastLogin === true) {
                        ele.addClass('active')
                        $('#company_name').text(v.companyName.length > 8 ? v.companyName.substr(0, 8) + '...' : v.companyName).addClass('change_company')
                        memberBaseInfoHeader(v.memberId)
                    }

                    companyEle.append(ele)
                })

                if (companyEle.find('.active').length == '0') {
                    changeCompany(0)
                } else {

                }
                $('#company_name').on('click', function() {
                    companyEle.show()
                    setTimeout(function() {
                        companyEle.css({ 'height': 36 * companyList.length + 'px' })
                    }, 100)
                    return false

                })
                $(document).on('click', function() {
                    companyEle.css('height', '0px')
                    setTimeout(function() {
                        companyEle.hide()
                    }, 600)
                })
            }

        })
        //     alert(3)
    $(".weidenglu_li").hide();
    $(".welcome").text("您好！")

    $(".yidenglu_li").show()
    $(".kfrx").hide();

}
/*  获取未读站内信 */
function getNewsList() {
    var query = {
        current: 1,
        size: 3,
        source: 0
    }
    utils.ajaxGet("/b2b-message/notice/system-notices", query, function(res) {
        var str = ''
        if (res.data.records.length < 1) {
            str = '<p class="aglinC">暂无未读消息</p>'
            $("#memberNews").html(str);
            return false;
        }
        $.each(res.data.records, function(index, item) {
            str += '<li data-id="' + item.id + '"><span>' + item.title + '</span><span>' + utils.formatterDateNews(item.createTime) + '</span><span>' + item.categoryName + '</span></li>'
        })
        $("#homeNewsLi").html(str);
        $(".news_num_font").text(res.data.total).show();
    }, function() {

    })
}

/* 获取企业基本信息 */
function memberBaseInfoHeader(memberId) {
    var data = {
        memberId: memberId
    }
    utils.ajaxGet("/member/member-base-info?" + $.param(data), '', function(res) {
        console.log(res.data)
            //$.cookie("member-base-info",JSON.stringify(res.data), { expires: 1, path: '/' })
        utils.setLoginCookie("member-info", res.data)
        if (location.pathname == '/' || location.pathname == '/index.html"') {
            $(".weiDl").hide();
            $(".YDL").show();
            var member_info = JSON.parse($.cookie("member-base-info"))
            if (member_info.logoPic) {
                $("#avatar").attr("src", member_info.logoPic)
            }
            $('.login_default').hide()
            $(".logo02_1").show().addClass("logo02_1_login")
            $("#qqt_name").text(member_info.companyName)
        }

    }, function() {

    })
}